// pages/market/order/detail.js
import api from '../../../utils/api'
import message from '../../../utils/message'

Page({

    /**
     * 页面的初始数据
     */
    data: {

        order_no:null,
        order_detail:null,
        sender_id:-1,
        has_load:false

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        if(options.order_no){
            this.setData({
                order_no: options.order_no
            })
        }else{

            message.show_error("订单编号异常" , function () {
                wx.navigateBack({
                  delta: 1,
                })
            });
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

        this.getShopOrderDetail();

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    getShopOrderDetail:function (response) {
        var me = this;
        
        if(!this.data.order_no){
            return ;
        }
        var params = {}
        params.order_no = this.data.order_no;

        message.show_loading();
        api.shop.getShopOrderDetail( params , function (response) {
            message.hide_loading();
            if( response.status==1 ){
                var detail = response.data;
                try{

                    var settle = JSON.parse( detail.settle );
                    detail.settle = settle;

                }catch(err){
                    detail.settle = null;
                }
                
                me.setData({
                    order_detail:detail,
                    has_load: true
                })
            }else{
                message.show_error( response.message )
            }
        })
    },

    doShopOrderConfirm:function () {
        var me = this;
        var params = {}
        params.order_no = this.data.order_no;

        message.show_loading();
        api.shop.doShopOrderConfirm( params , function (response) {
            message.hide_loading();

            if( response.status==1 ){
                message.show_success( response.message , function () {
                    me.getShopOrderDetail();   
                })
            }else{
                message.show_error( response.message )
            }
        })
        
    },
    doShopOrderComplete:function (e) {
        
        var order_no = e.currentTarget.dataset.order

        var me = this;
        var params = {}
        params.order_no = this.data.order_no;

        api.shop.doShopOrderComplete( params , function (response) {

            if( response.status==1 ){

                message.show_success( response.message , function () {
                    me.getShopOrderDetail();
                } )

            }else{
                message.show_error( response );

            }
            
        })
    },
    doShopOrderPay:function( e ){
        //去支付订单
        var me = this;
        
        var order_no  = this.data.order_no;

        var params = {}
        params.order_no = order_no;

        api.shop.doShopOrderPay( params , function (response) {
            if(response.status == 1){
                me.appletPay(response.data , order_no )

            }else{
                message.show_error( response.message );
            }
        } )

    },
    appletPay:function( payInfo , order_no ){
        //调用微信支付信息，勾起订单支付
        var me = this;
        wx.requestPayment({
          "timeStamp": payInfo.timeStamp.toString(),
          "nonceStr": payInfo.nonceStr,
          "package": payInfo.package,
          "signType": payInfo.signType,
          "paySign": payInfo.paySign,
          success:function( res ){
              console.log(res);
              //me.getOrderDetail(order_no);
              //me.getOrderList(0)
              //支付完成， 校验支付结果
              message.show_error("订单支付成功，商家确认后安排配送" , function(  ){
                 me.getShopOrderDetail();
              })

              //console.log(order_no)
          },
          fail:function(res){
              console.log(res)
          }
        })
    },
    doShopOrderCancel:function (e) {
        //取消店铺订单
        var me = this;

        var order_no =this.data.order_no;

        if(!order_no){
            message.show_error("订单编号异常");
        }

        var params = {};
        params.order_no = order_no;

        api.shop.doShopOrderCancel( params , function (response) {
            if(response.status==1){
                message.show_error(response.message , function () {
                    //刷新当前选中数据页
                    me.getShopOrderDetail();
                })
            }else{
                message.show_error(response);
            }
        });
        
    }

})